package org.spring.boot.func.factory.agent;

import org.spring.boot.func.properties.FuncProperties;
import org.spring.boot.func.utils.FuncString;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Yiur
 */
public class FuncLink {

    private static final String JAVA_PATH = "/src/main/java/";
    private FuncProperties funcProperties;
    private Map<Object, Object> classes;

    public FuncLink(FuncProperties funcProperties) {
        this.classes = new HashMap<>(10);
        this.funcProperties = funcProperties;
    }

    public void setFuncProperties(FuncProperties funcProperties) {
        this.classes = new HashMap<>(10);
        this.funcProperties = funcProperties;
    }

    public Map<Object, Object> getObjects() {
        return classes;
    }

    @SuppressWarnings("All")
    public <T> T getObject(@NonNull Class<T> classFile) {
        Assert.isTrue(funcProperties.isSingleton(), "The current mode is a singleton mode, it cannot be without the name attribute");
        return (T) classes.get(classFile);
    }

    @SuppressWarnings("All")
    public <T> T getObject(@NonNull Class<T> classFile, @NonNull String name) {
        return (T) classes.get(name);
    }

    public <T> void setObject(@NonNull Class<?> classFile, @NonNull T lambda) {
        Assert.notNull(lambda, "classFile not fund");
        Assert.isTrue(funcProperties.isSingleton(), "The current mode is a singleton mode, it cannot be without the name attribute");
        Assert.isTrue(funcProperties.isLambda() && classFile.getDeclaredMethods().length >= 1, "The current class file must declare a method, and there must be less than one method : lambda is true");
        classes.put(classFile, lambda);
    }

    public <T> FuncLink setObject(@NonNull Object key, @NonNull T lambda) {
        Assert.notNull(lambda, "classFile not fund");
        Assert.notNull(key, "key is null");
        Assert.isTrue(!key.equals(FuncString.STRING_EMPTY), "key is null");
        Assert.isTrue(funcProperties.isLambda() && lambda.getClass().getDeclaredMethods().length >= 1, "The current class file must declare a method, and there must be less than one method : lambda is true");
        classes.put(key, lambda);
        return this;
    }

    @Deprecated
    public <T> FuncLink setObject(@NonNull Class<?> classFile, @NonNull Object key, @NonNull T lambda) {
        Assert.notNull(lambda, "classFile not fund");
        Assert.notNull(key, "key is null");
        Assert.isTrue(!key.equals(FuncString.STRING_EMPTY), "key is null");
        Assert.isTrue(funcProperties.isLambda() && classFile.getDeclaredMethods().length >= 1, "The current class file must declare a method, and there must be less than one method : lambda is true");
        classes.put(key, lambda);
        return this;
    }

}
